package com.jandan.web.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.fckeditor.FCKeditor;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;

import com.jandan.logic.JWordzFacade;
import com.jandan.ui.client.util.ClientUtil;
import com.jandan.ui.model.BlogArticle;

import edu.emory.mathcs.backport.java.util.Arrays;

public class BlogArticleController extends MultiActionController {
	private JWordzFacade jwordz;

	public void setJwordz(JWordzFacade jwordz) {
		this.jwordz = jwordz;
	}

	public ModelAndView displayAll(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		String pageIndex = request.getParameter("pageIndex");
		int pi = ClientUtil.FIRST_PAGE_INDEX;
		int limit = ClientUtil.ARITCLE_PAGE_LIMIT;
		
		if(pageIndex != null){
			pi = Integer.parseInt(pageIndex);
		}
		
		int totalArticleCount = jwordz.getTotalBlogArticleCount();
		
		List<BlogArticle> articleList = jwordz.getAllBlogArticleList(pi * limit, limit);
		if (articleList == null) {
			articleList = new ArrayList<BlogArticle>();
		}

		Map map = new HashMap();
		
		map.put("totalArticleCount", totalArticleCount);
		map.put("articleList", articleList);
		
		return new ModelAndView("admin_blogArticle",map);
	}

	public ModelAndView displayForm(HttpServletRequest request,
			HttpServletResponse reponse) {
		String articleID = request.getParameter("articleID");

		ModelAndView model = new ModelAndView("admin_editBlogArticleForm");
		FCKeditor fckEditor = new FCKeditor(request, "content");
		fckEditor.setWidth("600px");
		fckEditor.setHeight("400px");

		if (articleID != null) {
			long id = Long.parseLong(articleID);
			BlogArticle article = jwordz.getBlogArticleByArticleID(id);
			if (article != null) {
				fckEditor.setValue(article.getContent());
			}
			model.addObject("article", article);
		}

		List<String> categoryList = generateCategoryList();
		
		model.addObject("fckEditor", fckEditor);
		model.addObject("categoryList", categoryList);

		return model;
	}

	public ModelAndView displayOne(HttpServletRequest request,
			HttpServletResponse reponse) {
		ModelAndView model = new ModelAndView("admin_viewBlogArticle");
		String articleID = request.getParameter("articleID");

		if (articleID != null) {
			long id = Long.parseLong(articleID);
			BlogArticle article = jwordz.getBlogArticleByArticleID(id);
			model.addObject("article", article);
		}

		return model;
	}

	public ModelAndView add(HttpServletRequest request,
			HttpServletResponse reponse) {
		BlogArticle article = generateArticle(request);			
		jwordz.insertBlogArticle(article);
		return new ModelAndView(new RedirectView("blog.htm"), "message",
				"博客文章添加成功");
	}

	public ModelAndView edit(HttpServletRequest request,
			HttpServletResponse reponse) {
		BlogArticle article = generateArticle(request);
		jwordz.updateBlogArticle(article);
		return new ModelAndView(new RedirectView("blog.htm"), "message",
				"博客文章修改成功");
	}

	private BlogArticle generateArticle(HttpServletRequest request) {
		String title = request.getParameter("title");
		String author = request.getParameter("author");
		String keywords = request.getParameter("keywords");
		String category = request.getParameter("category");
		String content = request.getParameter("content");
		String description = request.getParameter("description");
		String imageName = request.getParameter("imageName");

		BlogArticle article = new BlogArticle();
		
		String s = request.getParameter("articleID"); 
		if(s != null){
			article.setArticleID(Long.parseLong(s));	
		}
		
		article.setTitle(title);
		article.setAuthor(author);
		article.setCategory(category);
		article.setKeywords(keywords);
		article.setContent(content);
		article.setModifiedDate(new Date());
		article.setDescription(description);
		article.setImageName(imageName);
		return article;
	}

	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse reponse) {
		long articleID = Long.parseLong(request.getParameter("articleID"));
		jwordz.deleteBlogArticle(articleID);
		return new ModelAndView(new RedirectView("blog.htm"), "message",
				"博客文章删除成功");
	}
	
	private List<String> generateCategoryList(){
		return Arrays.asList(ClientUtil.BLOG_CATEGORIES);
	}
	
	
}
